package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	


	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().createContactPage();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION){
//			selectByText(By.name("new_group"), contact.group);
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0){
				throw new Error("Group selector exists in contact modification form");
			}
		}

		type(By.name("address2"), contact.getSecondaryAddress());
		type(By.name("phone2"), contact.getSecondaryPhone());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper goToCreateContactPage() {
		click(By.linkText("add new"));
		return this;
	}
	
	public ContactHelper initContactModification(int index) {
		click(By.xpath("//tr[@name = 'entry'][" + (index +1) + "]/td[7]/a/img"));
		return this;		
	}

	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		submitContactDeletion();
		returnToHomePage();
		manager.getModel().removeContact(index);
		return this;
		
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		manager.getModel().removeContact(index).addContact(contact);
		return this;
	}
	
	public ContactHelper submitContactDeletion(){
		click(By.xpath("//input[@value='Delete']"));
		return this;
	}
	
	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		return this;
	}

	
	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> contactsRows = driver.findElements(By.name("entry"));
		for (WebElement row : contactsRows) {
			ContactData contact = new ContactData();
			contact.withFirstname(row.findElement(By.xpath("td[3]")).getText())
			.withLastname(row.findElement(By.xpath("td[2]")).getText())
			.withEmail(row.findElement(By.xpath("td[4]/a")).getText())
			.withHomePhone(row.findElement(By.xpath("td[5]")).getText());
			contacts.add(contact);
		}
		
		return contacts;
	}

}
